unit uWMainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uWForm, cxPC, dxDockControl, dxDockPanel,
  Vcl.ComCtrls, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore, dxSkinBlack, dxSkinCaramel, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, cxContainer, cxEdit, Vcl.Menus, dxSkinscxPCPainter,
  cxPCdxBarPopupMenu, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  Vcl.StdCtrls, cxButtons, cxTextEdit, cxMaskEdit, cxDropDownEdit,
  cxImageComboBox, Vcl.ExtCtrls, cxSplitter, cxTreeView, dximctrl, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, System.Actions, Vcl.ActnList,
  dxSkinDevExpressStyle, dxSkinSeven, dxSkinSevenClassic, dxSkinVS2010,
  dxSkinsForm;

type
  TWMainForm = class(TWForm)
    dxDockSite1: TdxDockSite;
    dxDockPanel1: TdxDockPanel;
    dxLayoutDockSite1: TdxLayoutDockSite;
    cxTreeView1: TcxTreeView;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    Panel2: TPanel;
    cxPageControlForms: TcxPageControl;
    cxPagePrincipal: TcxTabSheet;
    Label1: TLabel;
    cxButton1: TcxButton;
    Panel3: TPanel;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxLookupComboBox1: TcxLookupComboBox;
    actNextPage: TAction;
    actPriorPage: TAction;
    dxSkinController1: TdxSkinController;
    procedure FormCreate(Sender: TObject);
    procedure actNextPageExecute(Sender: TObject);
    procedure actPriorPageExecute(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
  private
    procedure CarregaItemsMenu;
  protected
    procedure doClosePage; override;
  public
    class function GetInstance : TWMainForm;
    procedure SelecionaMenu(AFormName : string);
  end;

var
  WMainForm: TWMainForm;

implementation

uses
  uWIntf;

{$R *.dfm}

{ TWMainForm }

procedure TWMainForm.actNextPageExecute(Sender: TObject);
begin
  inherited;
  cxPageControlForms.SelectNextPage(True);
end;

procedure TWMainForm.actPriorPageExecute(Sender: TObject);
begin
  inherited;
  cxPageControlForms.SelectNextPage(False);
end;

procedure TWMainForm.CarregaItemsMenu;
begin
  // feito para carregar o menu
end;

procedure TWMainForm.cxButton1Click(Sender: TObject);
begin
  inherited;
  SelecionaMenu('TW2013EntityForm');
end;

procedure TWMainForm.doClosePage;
var
  form : IWForm;
begin
  inherited;
  if cxPageControlForms.ActivePage <> cxPagePrincipal then
  begin
    form := ctxFactory.FormOpen[cxPageControlForms.ActivePage.Name];
    if form <> nil then
    begin
      form.Close;
      try
        ctxFactory.RemoveFormOpen(cxPageControlForms.ActivePage.Name);
      finally
        form := nil;
      end;
    end;
    cxPageControlForms.ActivePage.Destroy;
  end;
end;

procedure TWMainForm.FormCreate(Sender: TObject);
begin
  inherited;
  CarregaItemsMenu;
end;

class function TWMainForm.GetInstance: TWMainForm;
begin
  Result := WMainForm;
end;

procedure TWMainForm.SelecionaMenu(AFormName: string);
var
  frm : IWForm;
  page : TcxTabSheet;
begin
  page := TcxTabSheet.Create(cxPageControlForms);
  page.PageControl := cxPageControlForms;
  frm := ctxFactory.GetForm(AFormName) as IWForm;
  TWForm(frm).Parent := page;
  TWForm(frm).Align := alClient;
  TWForm(frm).BorderStyle := bsNone;
  TWForm(frm).Visible := True;
  page.Caption := TWForm(frm).Caption;
  page.Name := Format('%s_%d', [frm.ObjectRef.ClassName, ctxFactory.CountFormOpen]);
  ctxFactory.AddFormOpen(page.Name, frm);
  cxPageControlForms.ActivePage := page;
end;

end.
